﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace August.UI
{
    public class Text : ShapeBase
    {
        private PointF location= new PointF(100, 100);
        public string Content = "默认文本";
        public PointF Location
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
                PointFs = new PointF[1] { location };
            }
        }
        public Font Font = new Font("黑体", 30f, FontStyle.Regular);
        public Color BackColor = Color.Transparent;
        public GraphicsPath BackGraphicPath = new GraphicsPath();

        public float Esize
        {
            get
            {
                return Font.Size;
            }
            set
            {
                Font = new Font(Font.Name, value, Font.Style);
            }
        }

        public Text(string content, PointF location, Font font)
        {
            PointFs = new PointF[1] { new PointF(100, 100) };
            Content = content;
            Location = location;
            Font = font;
          
        }

        public Text()
        {
            PointFs = new PointF[1] { new PointF(100, 100) };
        }
        internal override void OnWheelPaint(PaintEventArgs e)
        {
            Text text = this;
            PointF[] m_pointFs = new PointF[1] { new PointF(text.PointFs[0].X, text.PointFs[0].Y) };
            float esize = text.Font.Size;
            using (System.Drawing.Drawing2D.Matrix transform = new Matrix())
            {
                transform.Scale(Parent.Owner.ScaleX, Parent.Owner.ScaleY, System.Drawing.Drawing2D.MatrixOrder.Append);
                transform.Translate(Parent.Owner.OffsetX, Parent.Owner.OffsetY, System.Drawing.Drawing2D.MatrixOrder.Append);
                transform.TransformPoints(m_pointFs);
                esize = esize * Parent.Owner.ScaleX;
                text.TransForm = transform.Clone();
                text.LastPointFs = m_pointFs;
            }
            Font font = new Font(Font.Name, esize, Font.Style,GraphicsUnit.Pixel);
            GraphicsPath graphicsPath = new GraphicsPath();
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Near;
            graphicsPath.AddString(Content, font.FontFamily, (int)FontStyle.Regular, font.Size, m_pointFs[0], stringFormat);
            Console.WriteLine(font.Size.ToString());
            GraphicsPath backgraphicsPath = new GraphicsPath();
            SizeF sizeF = TextRenderer.MeasureText(e.Graphics,Content, font,new Size(0,0),TextFormatFlags.HorizontalCenter);
            RectangleF rectangleF = new RectangleF(m_pointFs[0], sizeF);
            backgraphicsPath.AddRectangle(rectangleF);
            BackGraphicPath = backgraphicsPath;
            e.Graphics.FillPath(new SolidBrush(text.BackColor), backgraphicsPath);
            e.Graphics.FillPath(new SolidBrush(text.m_DrawColor), graphicsPath);
            text.DrawRegion = new Region(graphicsPath);
            text.DrawGraphicsPath = graphicsPath;
            text.MoveGraphicPath = graphicsPath;
            base.OnWheelPaint(e);
        }

        internal override bool OnDownPaint(MouseEventArgs e)
        {
            m_isSelected = false;
            m_IsSelectedPoint = false;
            m_selectedPointIndex = -1;

            if (this.BackGraphicPath.IsVisible(e.X, e.Y))
            {
                m_isSelected = true;
                m_DrawColor = ShapeBase.ColorMouseDown;
                m_mouseDownMovePointFs = new PointF[PointFs.Length];
                PointFs.CopyTo(m_mouseDownMovePointFs, 0);
                int index = -1;
                m_IsSelectedPoint = IsHitPoint(e.X, e.Y, LastPointFs, ref index);
                m_selectedPointIndex = index;
                return true;
            }
            else
                return false;

        }

        internal override void OnMovePaint(MouseEventArgs e)
        {
            base.OnMovePaint(e);
            if (m_isSelected && IsUserShape)
            {
                Point point = e.Location;
                Matrix matrix = new Matrix();
                matrix.Translate((-m_mouseDownPointF.X + point.X) * 1f / Parent.Owner.ScaleX, (-m_mouseDownPointF.Y + point.Y) * 1f / Parent.Owner.ScaleY);
                m_mouseDownMovePointFs.CopyTo(PointFs, 0);
                matrix.TransformPoints(PointFs);
            }
        }

        protected bool IsHitPoint(int x, int y, PointF[] pointFs, ref int index)
        {
            index = -1;
            for (int k = 0; k < pointFs.Length; k++)
            {
                if (Math.Abs(pointFs[k].X - x) < 6 && Math.Abs(pointFs[k].Y - y) < 6)
                {
                    index = k;
                    return true;
                }
            }
            return false;
        }

    }
}
